package graph;

import java.util.ArrayList;

import platform.Platform;


@SuppressWarnings("all")
public class Node_System extends Node {

	public Node_System(ArrayList<TaskConfiguration> tcs) {
		super(tcs);
	}

		private void successorNodeConfigs(int number, int size,
				ArrayList<TaskConfiguration> nodeConfig,
				ArrayList<ArrayList<TaskConfiguration>> nodeConfigs) {
			int i = number;
			for (String key : taskConfigurations.get(i).successors().keySet()) {
				ArrayList<TaskConfiguration> nc = (ArrayList<TaskConfiguration>) nodeConfig
						.clone();
				nc.add(taskConfigurations.get(i).successors().get(key));
				if (i == size - 1) {
					nodeConfigs.add(nc);
				} else {
					successorNodeConfigs(i + 1, size, nc, nodeConfigs);
				}
			}
		}

	@Override
	public ArrayList<Node> successors() {
		ArrayList<Node> successors = new ArrayList<Node>();
		ArrayList<ArrayList<TaskConfiguration>> nodeConfigs = new ArrayList<ArrayList<TaskConfiguration>>();

		successorNodeConfigs(0, taskConfigurations.size(),
				new ArrayList<TaskConfiguration>(), nodeConfigs);

		for (ArrayList<TaskConfiguration> nc : nodeConfigs) {
			if (!nc.contains(null)) {
				successors.add(new Node_Scheduler(nc));
			}
		}
		return successors;
	}

		private void predecessorNodeConfigs(int number, int size,
				ArrayList<TaskConfiguration> nodeConfig,
				ArrayList<ArrayList<TaskConfiguration>> nodeConfigs, int activated) {
			int i = number;
			for (String key : taskConfigurations.get(i).predecessors().keySet()) {
				int active = activated;
				if (key == "Scheduled") {
					active = activated + 1;
				} 
				ArrayList<TaskConfiguration> nc = (ArrayList<TaskConfiguration>) nodeConfig
						.clone();
				nc.add(taskConfigurations.get(i).predecessors().get(key));
	
				if (i == size - 1) {
					if (active <= Platform.getInstance().m()) {
						nodeConfigs.add(nc);
					}
				} else {
					predecessorNodeConfigs(i + 1, size, nc, nodeConfigs, active);
				}
			}
		}

	@Override
	public ArrayList<Node> predecessors() {
		ArrayList<Node> predecessors = new ArrayList<Node>();
		ArrayList<ArrayList<TaskConfiguration>> nodeConfigs = new ArrayList<ArrayList<TaskConfiguration>>();

		predecessorNodeConfigs(0, taskConfigurations.size(),
				new ArrayList<TaskConfiguration>(), nodeConfigs, 0);

		for (ArrayList<TaskConfiguration> nc : nodeConfigs) {
			if (!nc.contains(null)) {
				predecessors.add(new Node_Scheduler(nc));
			}
		}
		return predecessors;
	}
	
}
